package org.my.stock.controller;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import org.apache.ignite.IgniteJdbcDriver;
import org.my.stock.util.MyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyVetoException;
import java.sql.*;
import java.util.List;

@Controller
public class MyXueQiuController {

    public boolean hasName(String name) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        Class.forName("org.apache.ignite.IgniteJdbcDriver");
        String url = "jdbc:ignite:thin://localhost:10800/public?lazy=true&userToken=dafu";
        Connection conn = DriverManager.getConnection(url);
        PreparedStatement stmt = conn.prepareStatement("SELECT id FROM public.my_type WHERE my_type_name = ?");
        stmt.setString(1, name);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            if (rs.getInt(1) > 0) {
                flag = true;
            }
        }
        rs.close();
        stmt.close();
        conn.close();
        return flag;
    }

    @CrossOrigin(origins = "https://xueqiu.com", maxAge = 3600)
    @RequestMapping(value = "/run_xue_qiu_type")
    public @ResponseBody
    String run_xue_qiu_type(ModelMap model, HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "code", required = false) String code) throws SQLException, ClassNotFoundException, PropertyVetoException {
        String jsonpCallback = request.getParameter("callback");
        if (code != null) {
            LinkedTreeMap m = MyUtil.getGson().fromJson(code, new TypeToken<LinkedTreeMap>() {}.getType());
            System.out.println(m.get("name"));
            if (!this.hasName(m.get("name").toString())) {
                Class.forName("org.apache.ignite.IgniteJdbcDriver");
                String url = "jdbc:ignite:thin://localhost:10800/public?lazy=true&userToken=dafu";
                Connection conn = DriverManager.getConnection(url);
                PreparedStatement statement = conn.prepareStatement("insert into public.my_type (my_type_name) values (?)");
                statement.setString(1, m.get("name").toString());
                System.out.println("***********");

                statement.executeUpdate();
                statement.close();
                conn.close();
            }

            return jsonpCallback + "({data:'true'})";
        }

        return jsonpCallback + "({data:'false'})";
    }
}


































































